/*
package com.consumer.config;

import com.netflix.discovery.CacheRefreshedEvent;
import com.netflix.discovery.DiscoveryClient;
import com.netflix.discovery.DiscoveryEvent;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.EurekaEvent;
import com.netflix.discovery.EurekaEventListener;
import com.netflix.loadbalancer.ServerListUpdater;
import java.util.Date;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.discovery.event.HeartbeatEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.util.StringUtils;

*/
/**
 * @author xiangminwen
 * @date 2023/11/21 10:57
 * {@link com.netflix.loadbalancer.PollingServerListUpdater}
 * {@link CacheRefreshedEvent} 刷新的实现
 * {@link DiscoveryEvent} 状态更新事件
 *//*

@Slf4j
public class EurekaEventServerListUpdater implements ServerListUpdater, EurekaEventListener, ApplicationListener<HeartbeatEvent> {

    private final EurekaClient eurekaClient;
    private volatile String appsHashCode;
    private UpdateAction updateAction;
    private volatile long timestamp;

    public EurekaEventServerListUpdater(EurekaClient eurekaClient) {
        this.eurekaClient = eurekaClient;
        // 注册当前对象作为EurekaEventListener
        this.eurekaClient.registerEventListener(this);
    }

    @Override
    public void start(UpdateAction updateAction) {
        this.updateAction = updateAction;
        this.updateAction.doUpdate();
        log.info("start ServerList Updater");
        // 延迟0秒后开始执行任务，每隔5秒执行一次
        */
/*Executors.newScheduledThreadPool(1)
            .scheduleAtFixedRate(() -> {
            this.updateAction.doUpdate();
        }, 0, 5, TimeUnit.SECONDS);*//*

    }

    @Override
    public void stop() {
        log.info("stop ServerList Updater");
        this.updateAction.doUpdate();
    }

    @Override
    public String getLastUpdate() {
        return new Date(timestamp).toString();
    }

    @Override
    public long getDurationSinceLastUpdateMs() {
        return 0;
    }

    @Override
    public int getNumberMissedCycles() {
        return 0;
    }

    @Override
    public int getCoreThreads() {
        return 0;
    }

    @Override
    public void onEvent(EurekaEvent event) {
        //当Eureka客户端更新时发送事件
        log.info("start ServerList eurekaEvent");
        if (event instanceof DiscoveryEvent) {
            if (Objects.nonNull(updateAction)) {
                this.updateAction.doUpdate();
                this.timestamp = ((DiscoveryEvent) event).getTimestamp();
            }
        }
    }

    */
/**
     * 同事上线和下线不同的服务（导致被没发现）
     *//*

    @Override
    public void onApplicationEvent(HeartbeatEvent event) {
        if (!(event.getSource() instanceof DiscoveryClient)) {
            return;
        }
        DiscoveryClient client = (DiscoveryClient) event.getSource();
        if (Objects.nonNull(updateAction)) {
            if (StringUtils.isEmpty(this.appsHashCode) || !appsHashCode.equals(client.getApplications().getAppsHashCode())) {
                log.info("start ServerList HeartbeatEvent, appsHashCode={}", client.getApplications().getAppsHashCode());
                this.updateAction.doUpdate();
                this.appsHashCode = client.getApplications().getAppsHashCode();
                this.timestamp = System.currentTimeMillis();
            }
        }
    }
}
*/
